<template>
  <div class="login-page">
    <div class="card">
      <div class="card-header">
        <h2>学生综合管理系统</h2>
      </div>
      <div class="card-body">
        <form @submit.prevent="Login">
          <div class="form-group">
            <label for="username">用户名</label>
            <input
                type="text"
                id="username"
                v-model="userId"
                placeholder="请输入用户名"
            />
          </div>
          <div class="form-group">
            <label for="password">密码</label>
            <input
                type="password"
                id="password"
                v-model="password"
                placeholder="请输入密码"
            />
          </div>
          <button type="submit" :disabled="loading">登录</button>
        </form>
      </div>
      <div v-if="errorMessage" class="error-message">
        <!--                {{ errorMessage }}-->
      </div>
    </div>
  </div>
</template>

<script>
import userManager from "@/UserManager";
import request from "@/utils/request";
import { Message } from "element-ui";

export default {
  data() {
    return {
      loading: false,
      name:'',
      userId: '',
      password: '',
      errorMessage: '', // 错误信息
    };
  },

  methods: {
    async Login() {
      this.loading = true;
      this.errorMessage = '';

      const LoginRequest = {
        name: this.name,
        account: this.userId,
        password: this.password
      };

      try {
        const res = await request.post("/login", LoginRequest);

        if (res.data.code === 200) {
          localStorage.setItem('token', res.data.data.token);
          userManager.setName(res.data.data.name);
          userManager.setUserId(res.data.data.account);
          userManager.setPermissions(res.data.data.role);
            this.avatarInitial = res.data.data.name.charAt(0);
            // 更新本地存储中的头像信息
            localStorage.setItem('avatarInitial', this.avatarInitial);

            // 手动触发更新
            this.$root.$emit('updateAvatar', this.avatarInitial);

            this.$router.push('/system');  // 登录成功后跳转到首页
        } else {
          this.errorMessage = '登录失败';  // 显示错误信息
          this.showError(this.errorMessage);
        }
      } catch (error) {
        this.errorMessage = '登录请求失败';
        this.showError(this.errorMessage);
      } finally {
        this.loading = false;
      }
    },

    showError(message) {
      Message.error({
        message,
        duration: 3000 // 持续时间为3000毫秒（3秒）
      });
    },
  }
};
</script>
<style scoped>
.login-page {
  padding: 30px;
  background-color: #f9f9f9;
  min-height: 100vh; /* 确保页面高度至少为视口高度 */
  display: flex;
  justify-content: center;
  align-items: center;
}

.card {
  max-width: 400px; /* 调整卡片宽度 */
  width: 100%;
  padding: 20px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  background-color: #fff;
  border-radius: 8px;
  overflow: hidden; /* 确保圆角生效 */
}

.card-header {
  padding: 8px 16px; /* 可以稍微减少内边距 */
  font-size: 12px; /* 使标题更小 */
  font-weight: bold;
  text-align: center;
}


.card-body {
  padding: 20px;
}

.form-group {
  margin-bottom: 1em;
}

label {
  display: block;
  margin-bottom: 0.5em;
  color: #333;
}

input {
  border: 1px solid #ccc;
  padding: 0.5em;
  width: 100%;
  box-sizing: border-box;
  border-radius: 4px; /* 添加圆角 */
}

button {
  padding: 0.7em;
  background-color: #409eff;
  border: none;
  color: #fff;
  cursor: pointer;
  border-radius: 5px;
  width: 100%;
}

button:hover {
  background-color: #66b1ff;
}

.error-message {
  color: red;
  font-weight: bold;
  text-align: center;
  margin-top: 10px;
}
</style>
